package com.matrix.im.interfaces.controller;

import com.matrix.api.im.dto.SendOrderMessageDTO;
import com.matrix.api.im.vo.ChatMessageVO;
import com.matrix.api.im.vo.ConversationVO;
import com.matrix.api.im.vo.SystemMessageVO;
import com.matrix.common.domain.PageResult;
import com.matrix.common.domain.Result;
import com.matrix.im.application.IMAppService;
import com.matrix.im.application.MessagePullService;
import com.matrix.im.application.SystemMessageAppService;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author 有点甜
 * @since 2025/8/2
 */
@RestController
@RequestMapping("/app/im")
@RequiredArgsConstructor
public class ImController {

    private final IMAppService imAppService;
    private final MessagePullService messagePullService;
    private final SystemMessageAppService systemMessageAppService;

    @GetMapping("/chat-history/{friendId}")
    public Result<PageResult<ChatMessageVO>> getChatHistory(
            @RequestHeader("X-User-ID") Long userId,
            @PathVariable Long friendId,
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "20") int pageSize) {
        return Result.success(imAppService.getChatHistory(userId, friendId, pageNum, pageSize));
    }

    @PostMapping("/message/send/order")
    public Result<Void> sendOrderMessage(
            @RequestHeader("X-User-ID") Long userId,
            @RequestBody @Valid SendOrderMessageDTO dto) {
        imAppService.sendOrderMessage(userId, dto);
        return Result.success();
    }

    @GetMapping("/conversations")
    public Result<PageResult<ConversationVO>> getConversations(
            @RequestHeader("X-User-ID") Long userId,
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "20") int pageSize) {
        return Result.success(imAppService.getConversations(userId, pageNum, pageSize));
    }

    @PutMapping("/conversations/{friendId}/read")
    public Result<Void> markConversationAsRead(
            @RequestHeader("X-User-ID") Long userId,
            @PathVariable Long friendId) {
        imAppService.markConversationAsRead(userId, friendId);
        return Result.success();
    }

    @GetMapping("/system-messages")
    public Result<PageResult<SystemMessageVO>> getSystemMessages(
            @RequestHeader("X-User-ID") Long userId,
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "10") int pageSize) {
        return Result.success(systemMessageAppService.getSystemMessages(userId, pageNum, pageSize));
    }

    @PutMapping("/system-messages/{messageId}/read")
    public Result<Void> markSystemMessageAsRead(
            @RequestHeader("X-User-ID") Long userId,
            @PathVariable String messageId) {
        systemMessageAppService.markAsRead(userId, messageId);
        return Result.success();
    }

    @GetMapping("/unread-count")
    public Result<Map<String, Long>> getUnreadMessageCount(@RequestHeader("X-User-ID") Long userId) {
        long privateUnread = imAppService.getTotalUnreadCount(userId);
        long systemUnread = systemMessageAppService.getUnreadCount(userId);
        return Result.success(Map.of("unreadCount", privateUnread + systemUnread));
    }

    @GetMapping("/messages/pull")
    public Result<List<ChatMessageVO>> pullNewMessages(
            @RequestHeader("X-User-ID") Long userId,
            @RequestParam(defaultValue = "0") long lastMsgSeq) {
        return Result.success(messagePullService.pullNewMessages(userId, lastMsgSeq));
    }
}
